<?php
    header('Access-Control-Allow-Origin:*');
    include_once '../model/model.php';

    $model = new model();
    $model->connect();

    $name =htmlspecialchars($_POST['name']);
    $desc = htmlspecialchars($_POST['desc']);
    $file = $_FILES['file'];


    if ((($file["type"] == "image/gif") || ($file["type"] == "image/jpeg") || ($file["type"] == "image/pjpeg"))
        && ($file["size"] < 200000)) {
        if ($file["error"] > 0)
        {
            echo "Return Code: " . $file["error"] . "<br />";
        } else {
            if (file_exists("../files/" . $file["name"]))
            {
                echo $file["name"] . " already exists. ";
            } else {
                $fileName = time() .'-'. $file["name"];
                move_uploaded_file($file["tmp_name"],
                "../files/" . $fileName );
            }
        }
    } else {
        echo "Invalid file";
        exit();
    }

    if (!$name) {
        echo "name is empty";
        exit();
    }

    if (!$desc) {
        echo "desc is empty";
        exit();
    }

    echo $model->saveCuisine($name,$desc,"../server/files/".$fileName);
    exit();